<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>角色授权</title>
	<!-- title -->
    <% include ../public/page_title.html%>
	<link rel="stylesheet" href="/public/admin/module/treetable-lay/treetable.css">
	<style>
		.layui-elem-quote{
			font-size:20px;
		}
	</style>
</head>
<body class="main_body">
	<blockquote class="layui-elem-quote">角色授权</blockquote>
	<div class="layui-form news_list">
		<form class="layui-form">
			<div style="padding:0 10px ;">
				<table id="table" class="layui-table" lay-filter="table"></table>
			</div>
			<div class="layui-form-item" style="padding:10px;padding-bottom:0px;text-align: right;">
				<p style="display:inline-block;color:coral;">角色:<%=name%></p>
				<button class="layui-btn" lay-submit lay-filter="submit">授权</button>
				<a href="<%=adminName%>/role" class="layui-btn layui-btn-primary">返回</a>
			</div>
		</form>
	</div>
	<script type="text/html" id="typeTpl">
		{{d.type==1?'<button class="layui-btn layui-btn-sm">模块</button>':d.type==2?'<button class="layui-btn layui-btn-normal layui-btn-sm">菜单</button>':'<button class="layui-btn layui-btn-danger layui-btn-sm">操作</button>'}}
	</script>
	<script type="text/html" id="sortTpl">
		<input style='width:50px;' type="text" value="{{d.sort}}"  data-id="{{d._id}}" placeholder="请输入" autocomplete="on" class="layui-input change-btn">
	</script>
	<script type="text/html" id="iconTpl">
		<i class="layui-icon {{d.icon}}"></i>
	</script>
	<script type="text/javascript" src="/public/admin/layui/layui.js"></script>
	<script type="text/javascript" src="/public/admin/js/index.js"></script>
	<script>
		layui.config({
			base:'/public/admin/module/'
		}).extend({
			treetable: 'treetable-lay/treetable'
		}).use(['form','layer','jquery','table','treetable'],function(){
			var form = layui.form,
				layer =layui.layer,
				table = layui.table,
				treetable = layui.treetable,
				$ = layui.jquery;
			var moduleList=JSON.parse('<%-list%>');
			var renderTable = function () {
				treetable.render({
					treeColIndex: 1,
					treeSpid: "0",
					height: 'full-150',
					treeIdName: '_id',
					treePidName: 'module_id',
					treeDefaultClose: false,
					treeLinkage: true,
					elem: '#table',
					id:'table',
					data: moduleList,
					page: false,
					limits:[1000],
					cols: [[
						{type: 'checkbox'},
						{field: 'module_name', title: '模块名称',width: 200,},
						{field: 'type', title: '模块', align:'center', templet:'#typeTpl',width:80,},
						{field: 'icon', title: '图标',templet:'#iconTpl',align:'center',width:80,},
						{field: 'action_name', title: '操作名称'},
						{field: 'url', title: '操作地址'},
					]],
					done: function () {
						//监听提交
						form.on('submit(submit)', function(data) {
							data.field._csrf = "<%=csrf%>";
							data.field.role_id = "<%=id%>";
							var access_node=[]
							var checkStatus = table.checkStatus('table');
							var datalsit=checkStatus.data;
							for(var i=0;i<datalsit.length;i++){
								access_node.push(datalsit[i]._id);
							}
							data.field.access_node=access_node;
							console.log(data.field)
							$.ajax({
								type:'post',
								data:data.field,
								dataType:'json',
								url:'<%=adminName%>/role/doauth',
								success:function(res){
									console.log(res);
									if(res.code===0){
										layer.msg(res.msg);
										setTimeout(function(){
											window.location.href = "<%=adminName%>/role/auth?id=<%=id%>&name=<%=name%>"
										},500)
									}else{layer.msg(res.msg);}
								},
								error:function(err){layer.msg('后台出错');}
							})
							return false;
						})
					}
				});
			};
			//渲染表格
			renderTable()
		})
	</script>
</body>
</html>
